//
//  LLUserAccount.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/29.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit



class LLUserAccount: NSObject,NSCoding {

    
    var access_token:String?
    
    var expires_in:NSTimeInterval = 0 {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    class func userLogin()-> Bool {
        return LLUserAccount.loadAccount() != nil
    }
    var uid:String?
    //好友名称
    var name:String?
    //用户头像地址
    var avatar_large:String?
    
    var expires_date:NSDate?
    
    override var description:String{
        //这里崩过
    return "access_token:\(access_token), expires_in:\(expires_in), uid:\(uid): expires_date:\(expires_date), name:\(name), avatar_large:\(avatar_large)"
//    return  "access_token:\(access_token), expires_in:\(expires_in), uid:\(uid): expires_date:\(expires_date), name:\(name), avatar_large:\(avatar_large)"
    }
    
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
        
    }
    func loadUserInfo(finish:(error:NSError?) ->()){
        LLNetworkTools.sharedInstance.loadUserInfo{ (result,error) ->()in
            if error != nil || result == nil {
                finish(error: error)
                return
            }
            print("-------\(result)")
            self.name = result!["name"]as?String
            self.avatar_large = result!["avatar_large"]as?String
            
            self.saveAccount()
            
            LLUserAccount.userAccount = self
            finish(error: nil)
//            print("\(LLUserAccount.userAccount?.name)")
            
        }
    }
    /**
    这里要注意
    - parameter value: 写错没法读取数据
    - parameter key:
    */
    override func setValue(value: AnyObject?,  forUndefinedKey key: String) {}
    
    static let accountPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/Account.plist"
    
    func saveAccount(){
        
        NSKeyedArchiver.archiveRootObject(self, toFile: LLUserAccount.accountPath)
    }
    
    private static var userAccount:LLUserAccount?
    
    class func loadAccount() -> LLUserAccount?{
        if userAccount == nil{
             userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(accountPath) as? LLUserAccount
        }
//        userAccount?.expires_date = NSDate(timeIntervalSinceNow: -100)
        if userAccount != nil && userAccount?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            print("账号有效")
            return userAccount
        }
        return nil
    }
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    //解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        name = aDecoder.decodeObjectForKey("name") as?String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as?String
        
    }
    
    
}
